package ${packageName}.service.impl;

import org.springframework.stereotype.Service;
import ${packageName}.dao.${mapperName};
import ${packageName}.entity.${entityName};
import ${packageName}.service.${serviceName};
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eco.common.commonbase.exception.ServiceException;
import ${packageName}.framework.config.mvc.SpringContext;
import ${packageName}.dto.add.${addReqDto};
import ${packageName}.dto.update.${updateReqDto};
import ${packageName}.dto.req.${queryPageReqDto};
import ${packageName}.dto.res.${queryResDto};
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;

import java.util.List;

@Slf4j
@Service
public class ${serviceImplName} extends ServiceImpl<${mapperName},${entityName}> implements ${serviceName} {

    /**
     * 分页查询
     */
    @Override
    public IPage<${queryResDto}> query${entityName}Page(${queryPageReqDto} reqDto) {
        Long userId = SpringContext.getSysUserInfo().getId();
        Page<${entityName}> page = baseMapper.selectPage(reqDto.iPageInfo(), Wrappers.lambdaQuery(${entityName}.class)
            .eq(${entityName}::getUserId,userId)
            .eq(${entityName}::getDeleted, false));
        return page.convert(${entityNameLower} -> {
            ${queryResDto} queryResDto = new ${queryResDto}();
            BeanUtils.copyProperties(${entityNameLower}, queryResDto);
            return queryResDto;
        });
    }

    /**
     * 添加
     */
    @Override
    public Boolean add${entityName}(${addReqDto} reqDto) {
        ${entityName} ${entityNameLower} = new ${entityName}();
        Long userId = SpringContext.getSysUserInfo().getId();
        BeanUtils.copyProperties(reqDto, ${entityNameLower});
        ${entityNameLower}.setCreator(userId);
        ${entityNameLower}.setUpdater(userId);
        return this.save(${entityNameLower});
    }

    /**
     * 编辑
     */
    @Override
    public Boolean update${entityName}(${updateReqDto} reqDto) {
        Long userId = SpringContext.getSysUserInfo().getId();
        ${entityName} ${entityNameLower} = this.getOne(Wrappers.lambdaQuery(${entityName}.class)
            .eq(${entityName}::getId, reqDto.getId())
            .eq(${entityName}::getDeleted, false)
            .eq(${entityName}::getUserId, userId));
        if (${entityNameLower} == null) {
            throw new ServiceException("编辑信息不存在");
        }
        BeanUtils.copyProperties(reqDto, ${entityNameLower});
        ${entityNameLower}.setUpdater(userId);
        return this.updateById(${entityNameLower});
    }

    /**
     * 批量删除
     */
    @Override
    public Boolean deleteBatch(List<Long> ids){
        Long userId = SpringContext.getSysUserInfo().getId();
        if(CollUtil.isEmpty(ids)){
            throw new ServiceException("请选择要删除的数据");
        }
        return this.update(Wrappers.lambdaUpdate(${entityName}.class)
            .set(${entityName}::getDeleted, true)
            .set(${entityName}::getUpdater, userId)
            .in(${entityName}::getId, ids)
            .eq(${entityName}::getUserId, userId));
    }
}